import { Component } from 'react'
import { withRouter } from 'react-router-dom'

class RouteCuard extends Component {

    componentDidMount() {
        // 添加一个监听器
        this.unListen = this.props.history.listen((...regs) => {
            this.props.onChange(this.props.location, ...regs, this.unListen)
        })

        // 设置阻塞，只能设置一个
        // 和alert一样的弹窗
        this.props.history.block('要跳转吗')
    }
    componentWillUnmount() {
        // 卸载监听器
        this.unListen()
    }
    render() {
        return this.props.children
    }
}

export default withRouter(RouteCuard)